{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 迷你 AlphaGo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.nn import Linear, ReLU, Sequential\n",
    "net = Sequential(\n",
    "        Linear(3, 8), # 第1层有8个神经元\n",
    "        ReLU(), # 第1层神经元的非线性函数是max(*,0)\n",
    "        Linear(8, 8), # 第2层有8个神经元\n",
    "        ReLU(), # 第2层的神经元的非线性函数是max(*,0)\n",
    "        Linear(8, 1), # 第3层有1个神经元\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试函数 $g()$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def g(x, y):\n",
    "    x0, x1, x2 = x[:, 0] ** 0, x[:, 1] ** 1, x[:, 2] ** 2\n",
    "    y0 = y[:, 0]\n",
    "    return (x0 + x1 + x2) * y0 - y0 * y0 - x0 * x1 * x2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "寻找合适的神经元的权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration #0: loss = -96.30583953857422\n",
      "iteration #100: loss = -919.5015258789062\n",
      "iteration #200: loss = -1245.6943359375\n",
      "iteration #300: loss = -1397.668212890625\n",
      "iteration #400: loss = -1516.2354736328125\n",
      "iteration #500: loss = -1696.0799560546875\n",
      "iteration #600: loss = -1796.1083984375\n",
      "iteration #700: loss = -1791.957763671875\n",
      "iteration #800: loss = -1662.0594482421875\n",
      "iteration #900: loss = -1674.72216796875\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from torch.optim import Adam\n",
    "\n",
    "optimizer = Adam(net.parameters())\n",
    "for step in range(1000):\n",
    "    optimizer.zero_grad()\n",
    "    x = torch.randn(1000, 3)\n",
    "    y = net(x)\n",
    "    outputs = g(x, y)\n",
    "    loss = -torch.sum(outputs)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    if step % 100 == 0:\n",
    "        print ('iteration #{}: loss = {}'.format(step, loss.item()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试神经网络的性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试输入: tensor([[-0.7584,  1.2248,  0.3077],\n",
      "        [-0.5507, -0.1883,  0.9677]])\n",
      "人工神经网络计算结果: tensor([[ 1.0772],\n",
      "        [ 0.9887]])\n",
      "g的值: tensor([ 1.2223,  0.9271])\n",
      "理论最优值: tensor([[ 1.1598],\n",
      "        [ 0.8740]])\n",
      "g的值: tensor([ 1.2291,  0.9403])\n"
     ]
    }
   ],
   "source": [
    "# 生成测试数据\n",
    "x_test = torch.randn(2, 3)\n",
    "print ('测试输入: {}'.format(x_test))\n",
    "\n",
    "# 查看神经网络的计算结果\n",
    "y_test = net(x_test)\n",
    "print ('人工神经网络计算结果: {}'.format(y_test))\n",
    "print ('g的值: {}'.format(g(x_test, y_test)))\n",
    "\n",
    "# 根据理论计算参考答案\n",
    "def argmax_g(x):\n",
    "    x0, x1, x2 = x[:, 0] ** 0, x[:, 1] ** 1, x[:, 2] ** 2\n",
    "    return 0.5 * (x0 + x1 + x2)[:, None]\n",
    "yref_test = argmax_g(x_test)\n",
    "print ('理论最优值: {}'.format(yref_test))\n",
    "print ('g的值: {}'.format(g(x_test, yref_test)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
